package com.fileoperation.user;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by Administrator on 2016/10/23.
 */

public class UserInfo {
    private boolean isLogin = false;
    private String accessToken = null;
    private String refreshToken = null;
    private String username = null;
    private String uuid = null;
    public static final String SHARED_PREFERENCE_NAME = "USER_INFO";
    public static final String ACCESS_TOKEN = "access_token";
    public static final String REFRESH_TOKEN = "refresh_token";
    public static final String UUID = "uuid";
    public static final String USER_NAME = "username";
    private static UserInfo instance = new UserInfo();

    public static UserInfo getInstance() {
        return instance;
    }

    public void initUserInfo(Context context){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        accessToken = sharedPreferences.getString(ACCESS_TOKEN, null);
        refreshToken = sharedPreferences.getString(REFRESH_TOKEN, null);
        username = sharedPreferences.getString(USER_NAME, null);
        uuid = sharedPreferences.getString(UUID, null);
        checkIsLogin();
    }

    public void saveUserData(Context context){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(ACCESS_TOKEN, accessToken);
        editor.putString(REFRESH_TOKEN, refreshToken);
        editor.putString(USER_NAME, username);
        editor.putString(UUID, uuid);
        editor.apply();
        checkIsLogin();

    }
    public void clearUserInfo(Context context){
        accessToken = null;
        refreshToken = null;
        username = null;
        uuid = null;
        isLogin = false;
        saveUserData(context);
    }




    private void checkIsLogin() {
        isLogin = accessToken != null && refreshToken != null;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

}